function check()
{
    var username = $("#username").val();
    if (username != "") {
        $.ajax({
            type: "GET",
            url:  "CheckAvailability",
            data: {
                target: username
            },
            success: function(response) {
                var status = $(response).find('status').text();
                if (status == 'failed') $("#availability").html("<span style='color:#F00;'>This username is not available!</span>");
                else $("#availability").html("<span style='color:#00ff00;'>This username is available!</span>");
            },
            dataType: "xml"
        });
    }
}

function validate_privilege()
{
    valid = true;
    
    return valid;
}

function validate_form ()
{
    valid = true;

    if ( $("#level").val() == "" ) {
        alert ( "Please fill in the 'Account level' box." );
        valid = false;
        focusIt("level");

    } else if ( $("#NRIC").val() == "" ) {
        alert ( "Please fill in the 'IC' box." );
        valid = false;
        focusIt("NRIC");

    } else if ( $("#firstName").val() == "" || $("#firstName").val() == "First Name") {
        alert ( "Please fill in the 'First name' box." );
        valid = false;
        focusIt("firstName");

    } else if ( $("#lastName").val() ==  "" || $("#lastName").val() ==  "Last Name") {
        alert ( "Please fill in the 'Last name' box." );
        valid = false;
        focusIt("lastName");

    } else if ( $('input:radio[name=sex]:checked').val() == null ) {
        alert ( "Please choose your Gender: Male or Female" );
        valid = false;
        focusIt("sex");

    } else if ( $("#countryCode").val() ==  "" ) {
       alert ( "Please fill in the 'Phone Country code' box." );
        valid = false;
        focusIt("countryCode");

    } else if ( $("#phone").val() == "" ) {
        alert ( "Please fill in the 'Phone number' box." );
        valid = false;
        focusIt("phone");

    } else if ( $("#country").val() == "" ) {
        alert ( "Please select 'Country of origin'." );
        valid = false;
        focusIt("country");

    } else if ( $("#city").val() == "" ) {
        alert ( "Please specify 'City of origin'." );
        valid = false;
        focusIt("city");

    } else if ( $("#email1").val() == "" ) {
        alert ( "Please fill in the 'Email address' box." );
        valid = false;
        focusIt("email1");

    } else if ( !($("#email1").val().indexOf(".") > 2 && $("#email1").val().indexOf("@") > 0 && $("#email1").val().indexOf(",") < 0) ) {
        alert ( "Please give a valid email address." );
        valid = false;
        focusIt("email1");

    } else if ( $("#email1").val() != $("#email2").val() ) {
        alert ( "The two emails do not match!" );
        valid = false;
        focusIt("email1");

    } else if ( $("#username").val() == "" ) {
        alert($("#chosenUsername").val());
        alert ( "Please fill in the 'Username' box." );
        valid = false;
        focusIt("username");

    } else if ( $("#username").val().length < 8 ) {
        alert ( "Please give a Username with at least 8 characters." );
        valid = false;
        focusIt("username");

    } else if ( $("#password1").val() == "" ) {
        alert ( "Please fill in the 'Password' box." );
        valid = false; 
        focusIt("password1");

    } else if ( $("#password1").val().length < 8 ) {
        alert ( "Please give a Password with at least 8 characters." );
        valid = false;
        focusIt("password1");

    } else if ( $("#password1").val() != $("#confirmpass").val() ) {
        alert ( "The two passwords do not match!" );
        valid = false;
        focusIt("password1");

    } if ( $("#adminPass").val() == "" ) {
        alert ( "Please fill in the 'Admin password' box." );
        valid = false;
        focusIt("adminPass");

    } else {

        var specialChars = "!@#$%^&*()+=-[]\';,./{}|\":<>?";
        var i;

        for (i = 0; i < $("#username").val().length; i++) {
            if (specialChars.indexOf($("#username").val().charAt(i)) != -1) {
                alert ( "Please give a Username with no special characters." )
                valid = false;
                focusIt("username");
                break;
            }
        }

        for (i = 0; i < $("#password1").val().length; i++) {
            if (specialChars.indexOf($("#password1").val().charAt(i)) != -1) {
                alert ( "Please give a Password with no special characters." )
                valid = false;
                focusIt("password1");
                break;
            }
        }

        var numChars = "1234567890";

        for (i = 0; i < $("#countryCode").val().length; i++) {
            if (numChars.indexOf($("#countryCode").val().charAt(i)) < 0) {
                alert ( "Please give a valid Country code." )
                valid = false;
                focusIt("countryCode");
                break;
            }
        }

        for (i = 0; i < $("#phone").val().length; i++) {
            if (numChars.indexOf($("#phone").val().charAt(i)) < 0) {
                alert ( "Please give a valid Phone number." )
                valid = false;
                focusIt("phone");
                break;
            }
        }
    }

    return valid;
}